<?php
namespace cms\modules\backend\controllers;

use Yii;
use cms\controllers\AuthController;
use app\models\Tags;
use common\component\exception\ErrMap;
use common\component\tool\Tool;
use common\models\OperationLog;


class TagsController extends AuthController
{

    /*列表*/
    public function actionTagsList()
    {
        if(isset($this->get['page'])){
            $currPage = intval($this->get['page']?$this->get['page']:1);
        }else{
            $currPage = 1;
        }
        $pageSize = Tool::getPageSize();

        if(isset($this->get['hot'])){
            $currPage = 1;
            $pageSize = intval($this->get['hot']?$this->get['hot']:30);
        }

        $tags = new Tags;
        $count = $tags::find()->where('app_id = :app_id', [':app_id' => $this->app_id])->count();
        $tagsList = $tags::find()
                        ->where('app_id = :app_id', [':app_id' => $this->app_id])
                        ->orderBy(['heat' => SORT_DESC, 'tid' => SORT_ASC])
                        ->offset($pageSize*($currPage-1))->limit($pageSize)
                        ->asArray()->all();
        // P($tagsList);
        $res = array(
            'list' => $tagsList,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        return Tool::showRes([0, '获取成功'], $res);
    }

    /*添加*/
    public function actionAddTag()
    {
        if(Yii::$app->request->isPost){
            $tags = new Tags;
            if($tags->addTag($this->post)){
                $backInfo = [0, '添加APP标签成功'];//返回信息
            }else{
                if($tags->hasErrors()){
                    $backInfo = ErrMap::getError(10005);//返回信息
                    $exception = $tags->getErrors();//异常信息
                    if(array_key_exists("repeatTagname", $exception)){
                        $backInfo = ErrMap::getError(10005, '此变量名已存在');//返回信息
                    };
                }
            }
        };

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
            return $res;
        };
    }

    /*修改标签的值*/
    public function actionModifyTag()
    {
        /*如果有数据，进行修改*/
        if(Yii::$app->request->isPost){
            $id = isset($this->post['GlobalVariable']['id'])?intval($this->post['GlobalVariable']['id']):'';
            if(!$id){
                $backInfo = ErrMap::getError(10005);//返回信息
            }else{
                $tags = new Tags;
                if($tags->modifyTag($this->post, $id)){
                    $backInfo = [0, '修改标签成功'];//返回信息
                }else{
                    if($tags->hasErrors()){
                        $backInfo = ErrMap::getError(10005);//返回信息
                        $exception = $tags->getErrors();//异常信息
                    }
                }
            }

            if(!isset($backInfo)){
                $backInfo = ErrMap::getError(10005);//返回信息
            }
            if(!isset($exception)){
                $exception = '';//异常信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
                return $res;
            };

        }

        return Tool::showRes(ErrMap::getError(10005));
    }

    /*删除标签*/
    public function actionDelTag()
    {
        $id = isset($this->get['id'])?$this->get['id']:'';
        if($id){
            $tags = new Tags;
            if($tags->deleteAll('tid = :id', [':id' => $id])){//删除成功
                $backInfo = [0, '删除标签成功'];//返回信息
            }else{//删除失败
                $backInfo = ErrMap::getError(10013, '删除标签失败');//返回信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), '') ) != 'boolean'){
                return $res;
            };

        }
        return Tool::showRes(ErrMap::getError(10005));

    }


}
